package com.bocai.dahuatech.ui.activity;

import android.content.SharedPreferences;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.widget.EditText;

import com.bocai.dahuatech.Bean.BaseBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseActivity;
import com.bocai.dahuatech.event.MainEvent;
import com.bocai.dahuatech.utils.SharePrefencesUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 修改公司名称
 */
public class UpdataCompanyNameActivity extends BaseActivity {
    @Bind(R.id.et_forgetpsd_phone)
    EditText etPhone;
    @Override
    protected int contentView() {
        return R.layout.activity_settingcompanyname;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        initToolbar(R.string.settingcompanyname);
        mToolbar.inflateMenu(R.menu.menu_regist_third);
        initEvent();
    }

    @Override
    protected void initEvent() {
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                //修改公司名称
                if (TextUtils.isEmpty(etPhone.getText().toString())) {
                    showToast(R.string.companynamenotnull);
                }else{
                    showLoading();
                    commit();
                }

                return false;
            }
        });

    }

    private void commit() {

        getTestApi().updataCompanyName(SharePrefencesUtil.getFID(UpdataCompanyNameActivity.this), etPhone.getText().toString())
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoading();
                        showToast(R.string.neterror);
                    }

                    @Override
                    public void onNext(BaseBean baseBean) {
                        hideLoading();
                        if(baseBean.getReturnNo().equals("1")){
                            showToast(baseBean.getReturnInfo());
                            SharedPreferences sharedPreferences=getSharedPreferences("user",MODE_PRIVATE);
                            SharedPreferences.Editor editor=sharedPreferences.edit();
                            editor.putString("companyName",etPhone.getText().toString());
                            editor.commit();
                            MainEvent mainEvent=new MainEvent("updateCompanyName");
                            EventBus.getDefault().post(mainEvent);
                            finish();
                        }else
                        {
                            showToast(baseBean.getReturnInfo());
                        }
                    }
                });
    }


}
